package com.luchao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import org.junit.Test;

public class jPush {

    //从官网获取的APP_KEY 与 MASTER_SECRET
    private static String APP_KEY = "48df053cfd6753c03777928f";
    private static String MASTER_SECRET = "ac7ce6ec0327fec6907d1e21";

    //极光推送>>Android
    //parm 参数:registrationId设备唯一标识  title推送标题  msg推送消息体
    public static void jpushAndroid_registrationId(Map<String, String> parm) {
        //创建JPushClient(极光推送的实例)
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        //推送的关键,构造一个payload
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.android())//指定android平台的用户
//                .setAudience(Audience.registrationId(parm.get("id")))//registrationId指定用户

                .setNotification(Notification.android(parm.get("msg"), parm.get("title"), parm))
                //发送内容
                .setOptions(Options.newBuilder().setApnsProduction(false).build())
                //这里是指定开发环境,不用设置也没关系
                .setMessage(Message.content(parm.get("msg")))//自定义信息
                .build();

        try {
            PushResult pu = jpushClient.sendPush(payload);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    //极光推送>>All所有平台
    //parm 参数:registrationId设备唯一标识   msg推送消息体
    public static void jpushAll(Map<String, String> parm) {
        //创建JPushClient
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        //创建option
        PushPayload payload = PushPayload.newBuilder()
                .setPlatform(Platform.all())  //所有平台的用户
                //.setAudience(Audience.all())//你项目中的所有用户
                .setAudience(Audience.registrationId(parm.get("id")))//registrationId指定用户
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder() //发送ios
                                .setAlert(parm.get("msg")) //消息体
                                .setBadge(+1)
                                .setSound("happy") //ios提示音
                                .addExtras(parm) //附加参数
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder() //发送android
                                .addExtras(parm) //附加参数
                                .setAlert(parm.get("msg")) //消息体
                                .build())
                        .build())
                .setOptions(Options.newBuilder().setApnsProduction(true).build())//指定开发环境 true为生产模式 false 为测试模式 (android不区分模式,ios区分模式)
                .setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
                .build();
        try {
            PushResult pu = jpushClient.sendPush(payload);
            System.out.println(pu.toString());
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void pushtest() {
        //自定义推送参数
        Map<String, String> parm = new HashMap<String, String>();
        //这里的id是,移动端集成极光并登陆后,极光用户的rid
        parm.put("id", "13065ffa4e7b3565443");
//        parm.put("title", "这是标题!");
        //设置提示信息,内容是文章标题
        parm.put("msg","xxx极光集成内容！！！！！成功!");
        jpushAndroid_registrationId(parm);
        //jpushAll(parm);
    }

    /**
     * 批量推送方法
     * @param list  用户集合
     * @param title 标题
     * @param content 内容
     */
    public void pushdemo(List list,String title,String content){

    }

    /**
     * 单个推送方法
     * @param SysUser user
     * @param title 标题
     * @param content 内容
     */
    public void pushdemo(String user,String title,String content){

    }

    /**
     * 安卓与ios平台，通过别名推送
     * @param alias     别名
     * @param content   推送内容
     * @param key       拓展字段key值
     * @param value     拓展字段value
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert(String alias, String content,String key,String value) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        .setAlert(content)
                        .addPlatformNotification(AndroidNotification.newBuilder().addExtra(key,value)
                                .build())
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setBadge(+1)
                                .setSound("default").addExtra(key,value)
                                .build())
                        .build())
                .build();
    }

    @Test
    public void alias_alert_test(){
        buildPushObject_all_alias_alert("myalias","这是内容",APP_KEY,MASTER_SECRET);
    }

    public static void main(String[] args) {
        buildPushObject_all_alias_alert("myalias","这是内容",APP_KEY,MASTER_SECRET);
    }

}
